﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BigStamp.Entity;
using System.ComponentModel;
using NPOI.HSSF.UserModel;
using System.IO;

namespace BigStamp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //
        }

        private void btnAddForm_Click(object sender, RoutedEventArgs e)
        {
            FormWindow formWindow = new FormWindow();
            formWindow.ShowDialog();
            Utility.SaveToDatabse();
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //OrderForm form=new OrderForm();

            btnPrint.IsEnabled = false;
            Utility.FormToPrint = (OrderForm)dataForms.SelectedItem;
            

            PrintWindow printWindow = new PrintWindow();
            printWindow.ShowDialog();
            dataForms.Items.Refresh(); ;

            
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Utility.SaveToDatabse();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dataForms.ItemsSource = Utility.GetForms();
            
        }

        private void dataForms_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dataForms.SelectedItem==null)
            {
                return;
            }
            if (((OrderForm)dataForms.SelectedItem).FormFinished)
            {
                btnPrint.IsEnabled = false;
            }
            else
            {
                btnPrint.IsEnabled = true;
            }
        }

        private void txtNumber_TextChanged(object sender, TextChangedEventArgs e)
        {
            //ICollectionView view = Utility.;
            dateForm.SelectedDate = null;
            ICollectionView view = Utility.GetForms();

                view.Filter = delegate(object item)
                {
                    return ((OrderForm)item).FormNum.Contains(txtNumber.Text);
                };
            
            //else
            //{
            //    view.Filter = null;
            //}
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            ICollectionView view = Utility.GetForms();
            view.Filter = null;
        }

        private void dateForm_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            ICollectionView view = Utility.GetForms();
            view.Filter = delegate(object item) 
            {
                return ((OrderForm)item).CreateDate.Date.Equals(dateForm.SelectedDate);
            };
        }

        private void dateForm_CalendarOpened(object sender, RoutedEventArgs e)
        {
            txtNumber.Text = string.Empty;
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            var forms = Utility.GetForms();
            //List<Product> products = Utility.GetAllProductsDetails();
            HSSFWorkbook workbook = new HSSFWorkbook();
            var sheet = workbook.CreateSheet("“寄件人大傻冒，收件人大傻逼。”他们用加粗的黑体奔跑着念诵。");
            sheet.CreateRow(0);

            sheet.GetRow(0).CreateCell(0).SetCellValue("订单号");
            sheet.GetRow(0).CreateCell(1).SetCellValue("创建时间");
            sheet.GetRow(0).CreateCell(2).SetCellValue("运单号");
            sheet.GetRow(0).CreateCell(3).SetCellValue("打印时间");
            sheet.GetRow(0).CreateCell(4).SetCellValue("姓名");
            sheet.GetRow(0).CreateCell(5).SetCellValue("单位");

            sheet.GetRow(0).CreateCell(6).SetCellValue("城市");
            sheet.GetRow(0).CreateCell(7).SetCellValue("地址");
            sheet.GetRow(0).CreateCell(8).SetCellValue("电话");
            sheet.GetRow(0).CreateCell(9).SetCellValue("邮编");
            sheet.GetRow(0).CreateCell(10).SetCellValue("是否完成");


            int i = 1;
            foreach (var f in forms)
            {
                //f = (OrderForm)f;
                sheet.CreateRow(i);
                sheet.GetRow(i).CreateCell(0).SetCellValue(((OrderForm)f).FormNum);
                sheet.GetRow(i).CreateCell(1).SetCellValue(((OrderForm)f).CreateDate.ToShortDateString());
                sheet.GetRow(i).CreateCell(2).SetCellValue(((OrderForm)f).TrackingNum);
                sheet.GetRow(i).CreateCell(3).SetCellValue(((OrderForm)f).PrintDate);
                sheet.GetRow(i).CreateCell(4).SetCellValue(((OrderForm)f).DestName);
                sheet.GetRow(i).CreateCell(5).SetCellValue(((OrderForm)f).DestTitle);
                sheet.GetRow(i).CreateCell(6).SetCellValue(((OrderForm)f).DestCity);
                sheet.GetRow(i).CreateCell(7).SetCellValue(((OrderForm)f).DestAddr);
                sheet.GetRow(i).CreateCell(8).SetCellValue(((OrderForm)f).DestTel);
                sheet.GetRow(i).CreateCell(9).SetCellValue(((OrderForm)f).DestZipCode);
                sheet.GetRow(i).CreateCell(10).SetCellValue(((OrderForm)f).FormFinished.ToString());
                i++;
            }

            FileStream fs = new FileStream(@"d:\aa.xls", FileMode.OpenOrCreate);
            workbook.Write(fs);
            //ExportToExcel(ms, "products");
            fs.Close();
            MessageBox.Show("Excel 输出完成");
        }



    }
}
